package web.View;

import domain.Node;
import domain.NodeView;
import domain.Users;
import org.apache.commons.beanutils.BeanUtils;
import service.User_Service;
import service.impl.User_ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/dataViewServlet")
public class dataViewServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置页面编码
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
/*        //获取页面数据
        String IMEI = request.getParameter("IMEI");*/
        //获取Map集合
        Map<String,String[]> map = request.getParameterMap();
        //封装Users对象
        Node node = new Node();
        try {
            BeanUtils.populate(node,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        //通过IMEI查询节点数据
        User_Service user_service = new User_ServiceImpl();
        List<NodeView> nodeViews = user_service.findNodeView(node.getIMEI());
        //创建数据和时间对象
        String _data = "[";
        StringBuilder data = new StringBuilder(_data);
        String _time = "[";
        StringBuilder time = new StringBuilder(_time);

        /*循环拼接数据字符串*/
        for(NodeView s:nodeViews){
            data.append(s.getData()+",");
            /*String test = s.getData_date().toString();*/
            time.append("'"+s.getData_date()+"'"+",");
        }
        data.append("]");
        time.append("]");


        request.setAttribute("data",data.toString());
        request.setAttribute("time",time.toString());
        request.setAttribute("node",node);

        request.getRequestDispatcher("/view/dataView.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
